﻿module Sustainalytics.Common {

    import Common = Sustainalytics.Common;

    interface ICompaniesScope extends ng.IScope {
        reportsList: any;
        reportTypes: ICommonDto[];
        generateReport: () => any;
        reportResponse: IReportNotification;
        toggleActivity: (show: boolean) => any;
        loading: boolean;
        reportTypeModel: ICommonDto;
        selectedCompany: ICompany;
        selectedComparativeGroup: ICommonDto;
        selectedReferenceUniverse: IGuidNameDto;
        selectedMatrix: IGuidNameDto;
        companies: IGuidNameDto[];
        comparativeGroups: ICommonDto[];
        matrixes: IGuidNameDto[];
        referenceUniverses: IGuidNameDto[];
        test: any;
    }

    class ReportsController {
        constructor(private $scope: ICompaniesScope, companyReportsFactory: IReportsFactory, notificationFactory: INotificationFactory, commonFactory: ICommonFactory, staticObjects: Common.IStaticObjects, reportNotificationFactory: IReportNotificationFactory, $rootScope, logFactory: ILogFactory) {
            var s = $scope;
            var init = () => {
                s.loading = true;
                s.reportTypes = staticObjects.reportTypes;
                s.comparativeGroups = staticObjects.comparativeGroups;
                s.companies = staticObjects.companies;
                s.matrixes = staticObjects.matrixes;
                s.referenceUniverses = staticObjects.referenceUniverses;
                s.selectedCompany = s.companies[0];
                s.selectedComparativeGroup = s.comparativeGroups[0];
                s.selectedReferenceUniverse = s.referenceUniverses[0];
                s.selectedMatrix = s.matrixes[0];
                s.loading = false;
            };
            $.when(init())
                .done(() => {
                    if ($rootScope.authData && $rootScope.authData.access_token) {
                        s.loading = true;
                        companyReportsFactory.getReport()
                            .success((reportNotification: IReportNotificationCamelCase) => {
                                s.reportResponse = { Created: reportNotification.created, Error: reportNotification.error, FilePath: reportNotification.filePath, Finished: reportNotification.finished, ReportType: reportNotification.reportType, Status: reportNotification.status };
                                $rootScope.reportNotification = s.reportResponse;
                                s.loading = false;
                            }).error((data, status) => {
                                s.loading = false;
                                if (data != "") {
                                    notificationFactory.error(data.ExceptionMessage, data.message);
                                } else {
                                    notificationFactory.error(status, staticObjects.getErrorMesageByStatusCode(status));
                                }
                            });
                    }
                });

            s.reportsList = [];
            s.generateReport = () => {
                s.loading = true;
                var filters: ICompanyReportFilter =
                    { company: s.selectedCompany, comparativeGroup: s.selectedComparativeGroup.id, matrixId: s.selectedMatrix.id, referenceUniverses: [s.selectedReferenceUniverse.id] };

                if (s.reportTypeModel.id == 1) {
                    var utcDate = new Date();
                    logFactory.log({ username: $rootScope.userInfo.name, reportType: s.reportTypeModel.name, startReportRequest: utcDate, endReportRequest: null, error: "", status: staticObjects.reportStatus.Requested });
                    companyReportsFactory.generateReport(filters)
                        .success(() => {
                            s.reportResponse = { Created: null, Error: null, FilePath: null, Finished: null, ReportType: s.reportTypeModel.name, Status: staticObjects.reportStatus.Running };
                            $rootScope.reportNotification = s.reportResponse;
                            s.loading = false;
                        }).error((data, status) => {
                            logFactory.log({ username: $rootScope.userInfo.name, reportType: s.reportTypeModel.name, startReportRequest: null, endReportRequest: utcDate, error: staticObjects.getErrorMesageByStatusCode(status, data), status: "" });
                            notificationFactory.error("", staticObjects.getErrorMesageByStatusCode(status, data));
                            s.loading = false;
                        });
                }
                else {
                    //other report type
                    notificationFactory.error("", "Not available");
                    s.loading = false;
                }
            };
        }
    }

    angular.module('shell').controller('ReportsController', ['$scope', 'companyReportsFactory', 'notificationFactory', 'commonFactory', 'staticObjects', 'reportNotificationFactory', '$rootScope', 'logFactory', ($scope, companyReportsFactory, notificationFactory, commonFactory, staticObjects, reportNotificationFactory, $rootScope, logFactory) =>
        new ReportsController($scope, companyReportsFactory, notificationFactory, commonFactory, staticObjects, reportNotificationFactory, $rootScope, logFactory)
    ]);
}